package by.grsu.mikhailava.page;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;

import by.grsu.mikhailava.app.TestWebApplication;
import by.grsu.mikhailava.entity.User;
import by.grsu.mikhailava.entity.UserRole;
import by.grsu.mikhailava.dao.UserDao;
import by.grsu.mikhailava.page.student.StudentPage;
import by.grsu.mikhailava.page.tutor.TutorPage;



public class Authorization extends WebPage{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4489270078972162999L;
	
	@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
	public Authorization() {
		super();
		final Form form = new Form("form");
		add(form);
		
		final TextField login = new TextField("login", new Model());
		final TextField password = new TextField("password", new Model());

		form.add(login);
		form.add(password);
		
		form.add(new SubmitLink("logIn") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				try {
					UserDao userDao = new UserDao(
							TestWebApplication.DATA_XML_FOLDER);
					User user = userDao.searchByLogin(login.getValue());
					if (user.getPassword().equals(password.getValue())) {
						if (user.getRole().equals(UserRole.tutor)) {
							setResponsePage(new TutorPage(user));

						} else {
							setResponsePage(new StudentPage(user));
						}
					} else {
						if (user != null) {
							
								setResponsePage(Authorization.class);

							
							}
								
								
								
							
						}
					
				} catch (Exception ex) {

				}
			}
		});
		
		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				setResponsePage(MyFirstWebPage.class);
			}
		});
		
	
		
		
		
		
		
	}

}
